package com.bocomm.tracevisualization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocomm.tracevisualization.api.result.CodeMessage;
import com.bocomm.tracevisualization.api.result.Result;
import com.bocomm.tracevisualization.api.service.ITerminalManagementService;
import com.bocomm.tracevisualization.api.entity.TerminalManagement;
import com.bocomm.tracevisualization.api.vo.req.QueryTerminalManagementReq;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static com.bocomm.tracevisualization.core.CmdExecutor.checkIpAddress;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxp
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/terminalManagement")
public class TerminalManagementController {


    @Autowired
    ITerminalManagementService iTerminalManagementService;


    /**
     * 终端管理列表查询
     * @return 终端管理列表
     */
    @PostMapping("/query")
    public Result<IPage<TerminalManagement>> query(@RequestBody QueryTerminalManagementReq queryTerminalManagementReq){
        IPage<TerminalManagement> page = new Page<>();

        QueryWrapper<TerminalManagement> queryWrapper = new QueryWrapper<>();
        if(Objects.nonNull(queryTerminalManagementReq)){
             page = new Page<>(queryTerminalManagementReq.getPageNum(),queryTerminalManagementReq.getSize());

            queryWrapper.lambda().like(StringUtils.hasText(queryTerminalManagementReq.getTerminalIp()),TerminalManagement::getTerminalIp,queryTerminalManagementReq.getTerminalIp());
            queryWrapper.lambda().eq(Objects.nonNull(queryTerminalManagementReq.getActive()),
                    TerminalManagement::getActive,queryTerminalManagementReq.getActive());
        }


        IPage<TerminalManagement> pageRecord = iTerminalManagementService.page(page, queryWrapper);

        return Result.success(pageRecord);
    }

    /**
     * 终端管理列表查询
     * @return 终端管理列表
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody TerminalManagement terminalManagement) throws SchedulerException {
        if(!Objects.nonNull(terminalManagement)){
            return Result.fail(CodeMessage.VALIDATE_ERROR);
        }
        if(!checkIpAddress(terminalManagement.getTerminalIp())){
            return Result.fail(CodeMessage.VALIDATE_ERROR.fillArgs("ip 格式错误。非IPv4格式。"));
        }
        terminalManagement.setActive(null);
        iTerminalManagementService.saveOrUpdateAndChack(terminalManagement);
        return Result.success();
    }

    /**
     * 终端管理开关
     * @return 终端管理列表
     */
    @PostMapping("/active")
    public Result active(@RequestBody TerminalManagement terminalManagement) throws SchedulerException {
        if(!Objects.nonNull(terminalManagement)|| Objects.isNull(terminalManagement.getId())||Objects.isNull(terminalManagement.getActive())){
            return Result.fail(CodeMessage.VALIDATE_ERROR);
        }
        TerminalManagement updateObject = new TerminalManagement();
        updateObject.setActive(terminalManagement.getActive());
        updateObject.setId(terminalManagement.getId());
        iTerminalManagementService.active(updateObject,terminalManagement.getTerminalIp());
        return Result.success();
    }

    /**
     * 终端管理删除操作
     * @return 终端管理列表
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody TerminalManagement terminalManagement) throws SchedulerException {
        if(!Objects.nonNull(terminalManagement) || Objects.isNull(terminalManagement.getId())){
            return Result.fail(CodeMessage.VALIDATE_ERROR);
        }
        iTerminalManagementService.deleteConfig(terminalManagement.getId());
        return Result.success();
    }
}
